home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: newbie question concerning fread
- Date: 25 Feb 1996 11:51:18 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Distribution: inet
- Message-ID: <4gqejmINNh5@keats.ugrad.cs.ubc.ca>
- References: <rblayney.824643669@extro>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
-
- In article <rblayney.824643669@extro>,
- Robert Blayney <rblayney@extro.ucc.su.OZ.AU> wrote:
- >
- >Hi,
- > I'm attempting to use the following function call
- > fread(&result, sizeof(int), 16, fp);
- > Unfortunately when I attempt to print the contents of the result array
- >I obtain all zeros, where result is declared: int *result. I've tried successfully
-
- There is your problem. Since result is already a pointer to an integer type,
- you don't need to apply the '&' address operator. If you do, you pass the
- address of the pointer itself, which is certainly not the array of integers
- that it points to. Practically, you end up clobbering the value of the pointer
- and writing some extra bytes to some variable that is stored just next to the
- pointer in memory (or cause a segmentation violation). Theoretically, what you
- are doing produces what is known as "undefined results", which means that any
- sort of screw up can happen according to the C standard.
-
- >to use fgets which works on text files but really need to use fread. I'm not sure
- >where the problem lies but when I've been printing the result array I've
- >been using printf("%-6.2d", *(result + i));
-
- This is okay. However, you might also avail yourself of using the following
- notation instead of *(result + i):
-
- result[i]
-
- The two are equivalent, since in C, an "E1[E2]", where E1 and E2 are
- expressions, is the same as *((E1) + (E2)). One neat effect of this definition
- is that if you have an array called 'a', and an index called 'i', you can refer
- to the ith element as either a[i] or i[a]. Hence both of these constructs give
- the letter 'x':
-
- char x = "abcx"[3];
- char x = [3]"abcx";
- char x = ["abcx"]3;
- char x = 3["abcx"];
-
- All that matters is that one operand is a pointer, and one an integer.
- --
-
-